#include "Protocol.hpp"
#include "Sock.hpp"

void Usage(string proc)
{
    cout << "Usage \r\t" << proc << "server_ip server_port" << endl;
}

int main(int argc, char *argv[])
{
    if (argc != 3)
        Usage(argv[0]);

    int sock = Sock::Socket();
    Sock::Connect(sock, argv[1], atoi(argv[2]));

    request_t req;
    memset(&req, 0, sizeof(req));
    cout << "Please Enter Number one: ";
    cin >> req._x;
    cout << "Please Enter Number two: ";
    cin >> req._y;
    cout << "Please Enter Operator: ";
    cin >> req._op;

    // 1. 字节型传输
    // write(sock, &req, sizeof(req));
    // 2. Json序列化传输
    string request_string = request_serialization(req);
    write(sock, request_string.c_str(), request_string.size());

    char buffer[1024] = {0};
    ssize_t s = read(sock, buffer, sizeof(buffer));
    if (s > 0)
    {
        response_t resp = {0, 0};
        buffer[s] = 0;
        response_deserialization(buffer, resp);
        cout << "response->code[0:success]:" << resp._code << endl;
        cout << "response->result:" << resp._result << endl;
    }
    return 0;
}
